package com.xiaoxin.experience.service.environment.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.xiaoxin.experience.api.environment.model.EnvironmentInfo;
import com.xiaoxin.experience.common.Property;
import com.xiaoxin.experience.util.JSONUtil;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author xiaoxin
 */
@Setter
@Getter
@TableName("tbl_env_info")
public class EnvironmentEntity implements Serializable
{
    private static final long serialVersionUID = -4424861003495797242L;

    @TableId(value = "remote_ip")
    private String remoteIp;

    @TableField(value = "prefix")
    private String prefix;

    @TableField(value = "force_replace")
    private Integer forceReplace;

    @TableField(value = "variables")
    private String variables;

    public EnvironmentEntity()
    {

    }

    public EnvironmentEntity(EnvironmentInfo environmentInfo)
    {
        this.remoteIp = environmentInfo.getRemoteIp();
        this.prefix = environmentInfo.getPrefix();
        this.forceReplace = environmentInfo.isForceReplace() ? 1 : 0;
        this.variables = JSONUtil.toJson(environmentInfo.getVariableList());
    }

    public EnvironmentInfo toInfo()
    {
        EnvironmentInfo environmentInfo = new EnvironmentInfo();
        environmentInfo.setRemoteIp(remoteIp);
        environmentInfo.setPrefix(prefix);
        environmentInfo.setForceReplace(forceReplace == 1 ? true : false);
        environmentInfo.setVariableList(JSONUtil.jsonToList(variables, Property.class));
        return environmentInfo;
    }
}
